define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wish_list/index' + location.search,
                    add_url: 'wish_list/add',
                    edit_url: 'wish_list/edit',
                    del_url: 'wish_list/del',
                    multi_url: 'wish_list/multi',
                    table: 'wish_list',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                pageSize: 50,
                pageList: [10, 25, 50, 'All'],
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title')},
                        {field: 'member.nickname', title: __('Member.nickname')},
                        {field: 'image', title: __('Image'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"10":__('Status 10'),"20":__('Status 20'),"30":__('Status 30')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},

                        {field: 'operate', title: __('Operate'), table: table,
                            buttons:[
                                {
                                    name: 'consent', text: '通过', title: '通过', icon: '', confirm: '确定通过审核吗？',
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    url: 'wish_list/consent',
                                    success:function(data, ret){

                                        $(".btn-refresh").trigger("click");
                                    },
                                    error:function(err){
                                        console.log(err);
                                    }
                                },
                                {
                                    name: 'disagree', text: '拒绝', title: '拒绝', icon: '', confirm: '确定拒绝通过吗？',
                                    classname: 'btn btn-xs btn-danger btn-ajax',
                                    url: 'wish_list/disagree',
                                    success:function(data, ret){

                                        $(".btn-refresh").trigger("click");
                                    },
                                    error:function(err){
                                        console.log(err);
                                    }
                                },
                            ],
                            events: Table.api.events.operate,    formatter: function (value, row, index) {


                                var that = $.extend({}, this);


                                var table = $(that.table).clone(true);


// $(table).data("operate-edit", null);

                                if(row.status==10){
                                    $(table).data("operate-consent", null)
                                    $(table).data("operate-disagree", null)
                                }else if(row.status==20){
                                    $(table).data("operate-disagree", null)
                                }else if(row.status==30){
                                    $(table).data("operate-consent", null)
                                    $(table).data("operate-disagree", null)
                                }

                                $(table).data("operate-del", null);
                                $(table).data('operate-edit',null);

                                that.table = table;


                                return Table.api.formatter.operate.call(that, value, row, index);}}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});